package com.tsd.base.service.impl;

import com.tsd.base.dao.BaseAssistDataMapper;
import com.tsd.base.dao.BaseAssistTypeMapper;
import com.tsd.base.entity.BaseAssistType;
import com.tsd.base.entity.BaseAssistTypeExt;
import com.tsd.base.service.BaseAssistTypeService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BaseAssistType 服务实现层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Service
public class BaseAssistTypeServiceImpl extends BaseServiceImpl implements BaseAssistTypeService {

    @Resource
    private BaseAssistTypeMapper baseAssistTypeMapper;
    @Resource
    private BaseAssistDataMapper baseAssistDataMapper;

    @Override
    public BaseAssistType getBaseAssistTypeById(Long id) throws Exception {
        return baseAssistTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public BaseAssistTypeExt queryBySid(String sid) throws Exception {
        return baseAssistTypeMapper.selectBySid(sid);
    }

    @Override
    public void deleteBaseAssistType(Long id, SysUser opUser) throws Exception {
        BaseAssistType old = baseAssistTypeMapper.selectByPrimaryKey(id);
        super.checkEmpty(old, "该记录不存在");
        super.autoInjectBaseData(old, opUser, this.TYPE_DELETE);
        baseAssistTypeMapper.updateByPrimaryKeySelective(old);
        // 删除分类同时删除分类关联数据
        baseAssistDataMapper.deleteByTypeSid(old.getSid());
    }

    @Override
    public void saveBaseAssistType(BaseAssistType record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getName(), "名称不能为空");
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            baseAssistTypeMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            baseAssistTypeMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<BaseAssistTypeExt> findBaseAssistTypes(Map<String, Object> params, PageBean page) throws Exception {
        return baseAssistTypeMapper.selectByPage(params, page);
    }

    @Override
    public List<BaseAssistTypeExt> findBaseAssistTypes(Map<String, Object> params) throws Exception {
        return baseAssistTypeMapper.selectByParams(params);
    }
}
